package com.ihy.dao.mybatis;


import com.ihy.entity.Subscribe;
import org.apache.ibatis.annotations.*;

/**
 * 邮箱订阅数据处理层
 * <p>
 * Mapper注解:
 * 声明当前接口为一个Mapper接口
 * 其中@Select, @Insert，@Delete,@Update分别代表了执行的真实SQL
 **/
@Mapper
public interface DaoSubscribe {
    /**
     * 查询方法通过userid得到邮箱信息.
     * <p>
     * Results注解:
     * 当数据库字段名与实体类对应的属性名不一致时，使用这个注解映射来将其对应起来。
     * <p>
     * Result注解:
     * property是实体类的属性名，column是数据库表的字段名。
     */
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "userId", column = "userid"),
            @Result(property = "email", column = "email"),
    })
    @Select("select * from subscribe where userid=#{userid}")
    //@Select("<script> select * from subscribe where <if test='userid != null'> userid=#{userid} </if> </script>")
    Subscribe query(String userid);

    //添加邮箱
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    @Insert("insert into subscribe (userid, email) values(#{userId},#{email})")
    int insert(Subscribe subscribe);

    //修改邮箱
    @Update("update subscribe set email=#{email} where userid=#{userId}")
    int update(Subscribe subscribe);

    //删除邮箱
    @Delete("delete from subscribe where userid=#{userId}")
    int delete(String userid);
}
